package com.ra.crm;

import java.util.Calendar;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.googlecode.cswish.annotation.Rest;
import com.googlecode.cswish.exception.ApplicationException;
import com.googlecode.cswish.struts.spring.GenericService;
import com.ra.util.SmsService;

@Service
public class ProprietaryReqService {
	
	@Resource
	private SmsService smsService;
	
	@Resource
	private GenericService genericService;
	
	@Rest(simpleServiceName = "anonymity", type = "free")
	public boolean addProprietaryReq(ProprietaryReq model, String verifyCode) {
		String existedVerifyCode = smsService.getVerifyCode(model.getPhone());
		boolean pass = StringUtils.isNotEmpty(existedVerifyCode) && existedVerifyCode.equals(verifyCode);
		if (!pass) {
			smsService.reduceTryCount(model.getPhone());
			throw new ApplicationException("手机校验码不正确");
		}
		
		model.setCreateDate(Calendar.getInstance().getTime());
		genericService.add(model);
		return true;
	}
}
